package com.spzx.product.controller;

import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.service.CategoryBrandService;
import com.spzx.product.vo.CategoryBrandVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author lynn
 * @since 2025-03-12
 */
@RestController
@RequestMapping("/categoryBrand")
@Tag(name = "分类品牌")
public class CategoryBrandController extends BaseController {

    @Autowired
    CategoryBrandService categoryBrandService;

    @GetMapping("/list")
    @Operation(summary = "查询分类品牌列表",description = "根据品牌名称和分类名称查询列表")
    public TableDataInfo list(@RequestParam(value = "brandId",required = false) Integer brandId, @RequestParam(value = "categoryId",required = false) Integer categoryId){
        startPage(); //分页
        List<CategoryBrandVo> list = categoryBrandService.selectCategoryBrandList(brandId,categoryId);
        return getDataTable(list);
    }

    @PostMapping
    @Operation(summary = "新增分类品牌")
    public AjaxResult add(@Parameter(description = "分类品牌") @RequestBody CategoryBrand categoryBrand){
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(categoryBrandService.insertCategoryBrand(categoryBrand));
    }

    @DeleteMapping("/{ids}")
    @Operation(summary = "删除分类品牌")
    public AjaxResult remove(@Parameter(description = "分类品牌id数组") @PathVariable("ids") List<Long> ids){
        return toAjax(categoryBrandService.removeBatchByIds(ids));
    }

    /**
     * 根据分类id查询品牌列表
     */
    @GetMapping("/brandList/{categoryId}")
    public AjaxResult getBrandByCategoryId(@PathVariable Integer categoryId){
        List<Brand> list = categoryBrandService.getBrandByCategoryId(categoryId);
        return AjaxResult.success(list);
    }

}
